package com.searchengine.controller;

import com.searchengine.entity.KeywordWeight;
import com.searchengine.entity.User;
import com.searchengine.service.KeywordWeightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 关键词权重控制器
 */
@RestController
@RequestMapping("/api/keyword-weights")
public class KeywordWeightController {

    @Autowired
    private KeywordWeightService keywordWeightService;

    /**
     * 添加关键词权重
     */
    @PostMapping
    public ResponseEntity<KeywordWeight> addKeywordWeight(
            @RequestAttribute("user") User user,
            @RequestBody Map<String, Object> requestData) {
        String keyword = (String) requestData.get("keyword");
        BigDecimal weight = new BigDecimal(requestData.get("weight").toString());
        Date startTime = (Date) requestData.get("startTime");
        Date endTime = (Date) requestData.get("endTime");
        
        KeywordWeight result = keywordWeightService.addKeywordWeight(user.getId(), keyword, weight, startTime, endTime);
        return ResponseEntity.ok(result);
    }

    /**
     * 更新关键词权重
     */
    @PutMapping("/{id}")
    public ResponseEntity<KeywordWeight> updateKeywordWeight(
            @PathVariable Long id,
            @RequestBody Map<String, Object> requestData) {
        BigDecimal weight = requestData.get("weight") != null ?
                new BigDecimal(requestData.get("weight").toString()) : null;
        Date endTime = requestData.get("endTime") != null ?
                (Date) requestData.get("endTime") : null;
        
        KeywordWeight result = keywordWeightService.updateKeywordWeight(id, weight, endTime);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除关键词权重
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteKeywordWeight(@PathVariable Long id) {
        keywordWeightService.deleteKeywordWeight(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据ID获取关键词权重
     */
    @GetMapping("/{id}")
    public ResponseEntity<KeywordWeight> getKeywordWeightById(@PathVariable Long id) {
        KeywordWeight weight = keywordWeightService.getKeywordWeightById(id);
        return ResponseEntity.ok(weight);
    }

    /**
     * 获取用户的所有关键词权重
     */
    @GetMapping("/user")
    public ResponseEntity<List<KeywordWeight>> getUserKeywordWeights(@RequestAttribute("user") User user) {
        List<KeywordWeight> weights = keywordWeightService.getUserKeywordWeights(user.getId());
        return ResponseEntity.ok(weights);
    }

    /**
     * 获取用户的某个关键词权重
     */
    @GetMapping("/user/keyword")
    public ResponseEntity<List<KeywordWeight>> getUserKeywordWeightByKeyword(
            @RequestAttribute("user") User user,
            @RequestParam String keyword) {
        List<KeywordWeight> weights = keywordWeightService.getUserKeywordWeightByKeyword(user.getId(), keyword);
        return ResponseEntity.ok(weights);
    }

    /**
     * 获取当前有效的关键词权重
     */
    @GetMapping("/current")
    public ResponseEntity<List<KeywordWeight>> getCurrentKeywordWeights(@RequestParam String keyword) {
        List<KeywordWeight> weights = keywordWeightService.getCurrentKeywordWeights(keyword);
        return ResponseEntity.ok(weights);
    }

    /**
     * 计算关键词总权重
     */
    @GetMapping("/total-weight")
    public ResponseEntity<BigDecimal> calculateTotalWeight(@RequestParam String keyword) {
        BigDecimal totalWeight = keywordWeightService.calculateTotalWeight(keyword);
        return ResponseEntity.ok(totalWeight);
    }

    /**
     * 检查关键词权重是否重叠
     */
    @PostMapping("/check-overlap")
    public ResponseEntity<Boolean> checkOverlap(
            @RequestAttribute("user") User user,
            @RequestBody Map<String, Object> requestData) {
        String keyword = (String) requestData.get("keyword");
        Date startTime = (Date) requestData.get("startTime");
        Date endTime = (Date) requestData.get("endTime");
        Long excludeId = requestData.get("excludeId") != null ?
                Long.valueOf(requestData.get("excludeId").toString()) : null;
        
        boolean overlap = keywordWeightService.checkOverlap(user.getId(), keyword, startTime, endTime, excludeId);
        return ResponseEntity.ok(overlap);
    }
}